package com.googlecode.aleverr.yafotkiclient.ui.activities;

import android.app.TabActivity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.ViewSwitcher;
import com.googlecode.aleverr.yafotkiclient.R;
import com.googlecode.aleverr.yafotkiclient.api.GlobalSettings;
import com.googlecode.aleverr.yafotkiclient.api.UserHome;

public class MainActivity extends TabActivity implements View.OnClickListener {

    private static final String TAG = "MainActivity";

    private static final int REQUEST_AUTH = 1;

    private static final int PROGRESS_BAR_INDEX = 0;
    private static final int TAB_HOST_INDEX = 1;

    private static final String HOME_TAB_TAG = "tabHome";
    private static final String ALBUMS_TAB_TAG = "tabAlbums";
    private static final String ALL_PHOTOS_TAB_TAG = "tabAllPhotos";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        findViewById(R.id.buttonSignout).setOnClickListener(this);

        GlobalSettings settings = GlobalSettings.getInstance(this);
        new ObtainUserHome().execute(settings.getAccessToken());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_AUTH && resultCode == RESULT_OK) {
            String accessToken = data.getStringExtra("accessToken");
            if (accessToken != null && !"".equals(accessToken)) {
                GlobalSettings.getInstance(this).setAccessToken(accessToken);
                new ObtainUserHome().execute(accessToken);
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.buttonSignout:
                GlobalSettings.getInstance(this).setAccessToken("");
                Intent intent = new Intent(MainActivity.this, AuthActivity.class);
                startActivityForResult(intent, REQUEST_AUTH);
                break;
        }
    }

    private void initTabs() {
        UserHome userHome = GlobalSettings.getInstance(this).getUserHome();

        TabHost tabHost = getTabHost();
        TabHost.TabSpec tabSpec;
        View view;

        tabSpec = tabHost.newTabSpec(HOME_TAB_TAG);
        view = getLayoutInflater().inflate(R.layout.main_tab_header, null);
        ((TextView) view.findViewById(R.id.headerText)).setText("Home");
        tabSpec.setIndicator(view);
        tabSpec.setContent(R.id.tabHome);
        tabHost.addTab(tabSpec);

        tabSpec = tabHost.newTabSpec(ALBUMS_TAB_TAG);
        view = getLayoutInflater().inflate(R.layout.main_tab_header, null);
        ((TextView) view.findViewById(R.id.headerText)).setText("Albums");
        tabSpec.setIndicator(view);
        Intent intent = new Intent(this, AlbumsActivity.class);
        intent.putExtra("albumCollectionAddress", userHome.getAlbumCollectionAddress());
        tabSpec.setContent(intent);
        tabHost.addTab(tabSpec);

        tabSpec = tabHost.newTabSpec(ALL_PHOTOS_TAB_TAG);
        view = getLayoutInflater().inflate(R.layout.main_tab_header, null);
        ((TextView) view.findViewById(R.id.headerText)).setText("All Photos");
        tabSpec.setIndicator(view);
        intent = new Intent(this, PhotoGridActivity.class);
        intent.putExtra("photoCollectionAddress", userHome.getPhotoCollectionAddress());
        intent.putExtra("albumId", "");
        tabSpec.setContent(intent);
        tabHost.addTab(tabSpec);

        tabHost.setCurrentTab(0);
    }

    private void onAuthorized() {
        initTabs();

        findViewById(R.id.buttonSignout).setEnabled(true);

        ViewSwitcher viewSwitcher = (ViewSwitcher) findViewById(R.id.viewSwitcher);
        viewSwitcher.setDisplayedChild(TAB_HOST_INDEX);
    }

    private class ObtainUserHome extends AsyncTask<String, Void, UserHome> {
        @Override
        protected UserHome doInBackground(String... strings) {
            if (strings.length > 0) {
                String accessToken = strings[0];
                if (accessToken != null && !"".equals(accessToken)) {
                    return UserHome.obtainUserHome(null, accessToken);
                }
            }
            return null;
        }

        @Override
        protected void onPreExecute() {
            ((ViewSwitcher) findViewById(R.id.viewSwitcher)).setDisplayedChild(PROGRESS_BAR_INDEX);
        }

        @Override
        protected void onPostExecute(UserHome result) {
            if (result != null) {
                GlobalSettings.getInstance(MainActivity.this).setUserHome(result);
                onAuthorized();
            } else {
                Intent intent = new Intent(MainActivity.this, AuthActivity.class);
                startActivityForResult(intent, REQUEST_AUTH);
            }
        }
    }
}
